extern crate serde_json;

use self::serde_json::{json, Value};
use super::{do_cache, do_cache_ret, do_node, do_node_ret};
use base::base::{VBase, VMap};
use std::{thread, time};

#[allow(dead_code)]
pub async fn ck_task(p: String, val: Value) {
    let ten_millis = time::Duration::from_millis(30);
    thread::sleep(ten_millis);

    let cmd = vec![
        "all", "y4", "y5", "m41", "m42", "m43", "m52", "d411", "d412", "d413", "d423", "d523",
        "run", "task1", "task2", "task3", "task4",
    ];

    let val = val.kobj().unwrap();

    for d in cmd.iter() {
        //println!("===> {d}");

        let m = task_cmd(d);

        match val.k_val(d) {
            Some(v) => {
                do_node_ret(p.clone(), m.clone(), json!(v)).await;
                do_cache_ret(p.clone(), m, json!(v)).await;
            }
            None => {
                do_node(p.clone(), m.clone()).await;
                do_cache(p.clone(), m).await;
            }
        }
    }
}

#[allow(dead_code)]
pub fn task_cmd(cmd: &str) -> Value {
    match cmd {
        "all" => json!({"line": "1", "tp": "task", "sub": "all"}),
        "run" => json!({"line": "1", "tp": "task", "sub": "run"}),
        "task1" | "task2" | "task3" | "task4" => json!({"line": "1", "tp": "task", "sn": cmd}),
        "y4" => json!({"line": "1", "tp": "task", "sub": "year", "sn": "2024"}),
        "y5" => json!({"line": "1", "tp": "task", "sub": "year", "sn": "2025"}),
        "m41" => json!({"line": "1", "tp": "task", "sub": "month", "sn": "202401"}),
        "m42" => json!({"line": "1", "tp": "task", "sub": "month", "sn": "202402"}),
        "m43" => json!({"line": "1", "tp": "task", "sub": "month", "sn": "202403"}),
        "m52" => json!({"line": "1", "tp": "task", "sub": "month", "sn": "202502"}),
        "d411" => json!({"line": "1", "tp": "task", "sub": "day", "sn": "20240101"}),
        "d412" => json!({"line": "1", "tp": "task", "sub": "day", "sn": "20240102"}),
        "d413" => json!({"line": "1", "tp": "task", "sub": "day", "sn": "20240103"}),
        "d423" => json!({"line": "1", "tp": "task", "sub": "day", "sn": "20240203"}),
        "d523" => json!({"line": "1", "tp": "task", "sub": "day", "sn": "20250203"}),
        _ => panic!("tech_cmd"),
    }
}

#[allow(dead_code)]
pub fn task_data() -> Value {
    json!({
        "y": ["2024", "2025"],
        "y4": ["2024"],
        "y5": ["2025"],
        "m1": ["01"],
        "m2": ["02"],
        "m3": ["03"],
        "m12": ["01", "02"],
        "d1": ["01"],
        "d2": ["02"],
        "d3": ["03"],
        "d12": ["01", "02"],
        "d13": ["01", "03"],
        "t": ["task1", "task2"],
        "t1": ["task1"],
        "t2": ["task2"],
        "run1": "2024-01-01 01:01:01#task1",
        "task1": {
            "sn": "task1",
            "pstart": "2024-01-01 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        },
        "run1__4112": "2024-01-01 02:01:01#task1",
        "task1_4112": {
            "sn": "task1",
            "pstart": "2024-01-01 02:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        },
        "task1_n": {
            "sn": "task1",
            "pstart": "2024-01-01 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 4,
            "state": "open",
        },
        "task1_nc": {
            "sn": "task1",
            "pstart": "2024-01-01 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 4,
            "state": "close",
        },
        "task1_nc_413": {
            "sn": "task1",
            "pstart": "2024-01-03 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 4,
            "state": "close",
        },
        "task1_nc_423": {
            "sn": "task1",
            "pstart": "2024-02-03 01:01:01",
            "pend": "2026-02-03 01:01:01",
            "tech": "te1",
            "pnum": 4,
            "state": "close",
        },
        "task1_nc_523": {
            "sn": "task1",
            "pstart": "2025-02-03 01:01:01",
            "pend": "2026-02-03 01:01:01",
            "tech": "te1",
            "pnum": 4,
            "state": "close",
        },
        "run2": "2024-01-01 01:01:01#task2",
        "task2": {
            "sn": "task2",
            "pstart": "2024-01-01 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        },
        "run2_4113": "2024-01-01 03:01:01#task2",
        "task2_4113": {
            "sn": "task2",
            "pstart": "2024-01-01 03:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        },
        "task2_c4113": {
            "sn": "task2",
            "pstart": "2024-01-01 03:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "close",
        },
        "run2_412": "2024-01-02 01:01:01#task2",
        "task2_412": {
            "sn": "task2",
            "pstart": "2024-01-02 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        },
        "run3": "2024-02-02 01:01:01#task3",
        "task3": {
            "sn": "task3",
            "pstart": "2024-02-02 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        },
        "run4": "2025-02-02 01:01:01#task4",
        "task4": {
            "sn": "task4",
            "pstart": "2025-02-02 01:01:01",
            "pend": "2026-01-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        },
    })
}
